<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 16-11-3
 * Time: 下午4:19
 */

namespace utils\x1;


class MyFunction
{
    public static function buildInsertSql($table, $arrData)
    {
        $name = $values = '';
        $flag = $flagV = 1;
        $true = is_array(current($arrData));//判断是否一次插入多条数据
        if ($true) {
            //构建插入多条数据的sql语句
            foreach ($arrData as $arr) {
                $values .= $flag ? '(' : ',(';
                foreach ($arr as $key => $value) {
                    if ($flagV) {
                        if ($flag) $name .= "$key";
                        $values .= "'$value'";
                        $flagV = 0;
                    } else {
                        if ($flag) $name .= ",$key";
                        $values .= ",'$value'";
                    }
                }
                $values .= ') ';
                $flag = 0;
                $flagV = 1;
            }
        } else {
            //构建插入单条数据的sql语句
            foreach ($arrData as $key => $value) {
                if ($flagV) {
                    $name = "$key";
                    $values = "('$value'";
                    $flagV = 0;
                } else {
                    $name .= ",$key";
                    $values .= ",'$value'";
                }
            }
            $values .= ") ";
        }

        $strSql = "insert into $table ($name) values $values";

        return $strSql;
    }

    public static function array_unique_2d($array2D)
    {
        $temp = $res = array();
        foreach ($array2D as $v) {
            $v = json_encode($v);  //降维,将一维数组转换字符串

            $temp[] = $v;
        }
        $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
        foreach ($temp as $item) {

            $res[] = json_decode($item, true);   //再将拆开的数组重新组装
        }
        return $res;
    }

    public static function msectime()
    {
        list($tmp1, $tmp2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($tmp1) + floatval($tmp2)) * 1000);
    }

    // between 拼接
    public static function betweenSqlByTime($field, $start_time, $end_time)
    {


/*
        if ($start_time != '' && ($start_time == $end_time)) {

            $end_time = $start_time + 24 * 60 * 60;
        }*/

        if ($end_time !='') {

            $end_time = $end_time + 24 * 60 * 60;
        }

        return self::betweenSql($field, $start_time, $end_time);
    }

    // between 拼接
    public static function betweenSql($field, $start_value, $end_value)
    {
        $sql = '';
        $sql_param = array();
        if ($start_value == '' && $end_value == '') {

            $sql = '';
        } elseif ($start_value != '' && $end_value == '') {

            $sql = "'%d'<= $field";
            $sql_param = array(
                $start_value
            );
        } elseif ($start_value == '' && $end_value != '') {

            $sql = "$field<='%d'";
            $sql_param = array(
                $end_value
            );
        } else {

            $sql = "('%d'<= $field And $field<'%d')";
            $sql_param = array(
                $start_value,
                $end_value
            );
        }
        // print_r($sql_param);
        return self::escapeSql($sql, $sql_param);
    }

    /**
     * 转义，防止注入
     */
    public static function escapeSql($sql, $params)
    {
        $escape_params = array();

        foreach ($params as $k => $v) {

            $v = addslashes($v); // mysql_real_escape_string

            array_push($escape_params, $v);
        }

        return $sql = vsprintf($sql, $escape_params);
    }

    //将下划线命名转换为驼峰式命名
    public static function convertUnderline3($str, $ucfirst = true)
    {
        $str = ucwords(str_replace('_', ' ', $str));
        return $str = str_replace(' ', '', lcfirst($str));
    }

}
